/**
 * blank
 */
define(function(require) {
	var $ = require('jquery');
	var base = require('base');
	var com=require('./common');
  
    if(localStorage.getItem("lang")){
		var Lang = localStorage.getItem("lang")
	}else{
		var Lang = "zh_CN"
	}

	// 中文/英文转换
	if(Lang != "zh_CN"){
		$('#news_filter').html("News Screening")
		$('#keyword').attr("placeholder", "Please enter a keyword")
		
	}    

	let cateData = {
		nav_id: 12,
		keyword: '',
		page:1,
		list_row:10
	}


	// 切换类目
	changeCate = function(id){
		cateData.nav_id = id
		cateData.page = 1

		let get_cate_width =  $('#getCate').width();
		let offset_left= $('#getCate').offset().left;

		if(id == 13){
			// $('.xc_nav_line i').css("width","20%")
			$('.xc_nav_line i').css("width",offset_left + get_cate_width);
		}else{
			// $('.xc_nav_line i').css("width","13%")
			$('.xc_nav_line i').css("width",offset_left + get_cate_width/2);
		}
		getArticle()
		return false;
	}


	// 获取新闻分类
	$.ajax(seajs.ajaxroot + "getCate",{
		type:'post',
		headers:{
			'locale-language':Lang
		},
		success:function(res){	
			console.log("getCate:", res)
			let cate = '';
			res.data.forEach((item,index)=>{
				if(index == 0){
					cate += '<li class="cur">';
				}else{
					cate += '<li>'
				}
				cate += '<a onclick="changeCate('+item.id+')">'+item.title+'</a></li>';
			})
			$('#getCate').html(cate)
			let get_cate_width =  $('#getCate').width();
			let offset_left= $('#getCate').offset().left;
			$('.xc_nav_line i').css("width",offset_left + get_cate_width/2);
			


			getArticle();
		},
		error:function(err){
			console.log("error:", err)
		}

	});


	// 翻页
	changePage = function(page){
		cateData.page = page;
		getArticle();
	}


	getArticle = function(){
		cateData.keyword = $('#keyword').val();
		// 获取分类的新闻列表
		$.ajax(seajs.ajaxroot + "getArticle",{
			type:'post',
			async:false,
			data:cateData,
			headers:{
				'locale-language':Lang
			},
			success:function(res){
				console.log('getArticle:', res)
				let htmlData = '';
				res.data.forEach((item,index)=>{

					if(index < 2){
						let top_news = '<a href="/detail.html?id='+item.id+'"><div class="box"><div class="date">'+item.date+'</div>';
						top_news += '<div class="tit">'+item.title+'</div>';
						top_news += '<div class="con el">'+item.description+'</div>';
						top_news += '<div class="common-btn"><i class="iconfont" data-code="xe611"></i></div></div></a>';

						if(index ==0) {$('#info_left').html(top_news)};
						if(index ==1) {$('#info_right').html(top_news)};
						
					}



					htmlData += '<li>';
					htmlData += '<a href=/detail.html?id='+item.id+' class="fix imgZoom">';
					htmlData += '<div class="left"><div class="tit el">'+item.title+'</div>';
					htmlData += '<div class="con">'+item.description+'</div>';
					htmlData += '<div class="txt"><div class="date">'+item.date+'</div><div class="common-btn"><i class="iconfont" data-code="xe611"></i></div></div></div>'
					if(item.main_img != "" ){
						htmlData += '<div class="right"><div class="rect-625"><img src="'+item.main_img+'"></div></div>';		
					}
					htmlData += '</a></li>';
				})
				
				$('#newsList').html(htmlData);

				let pageList = '';
				let total = res.total;
				// let total = 20;
				for (let index = 1; index <= total; index++) {										
					if(index == cateData.page){
						pageList += '<span class="num current">&nbsp;'+index+'&nbsp;</span>';
						continue;
					}
					if(index >= 6 && index < total-2){
						continue;					
					}
					if(index == (total -2)){
						pageList += '<a href="javascript:void(0);" >...</a>';
						continue;
					}
					pageList += '<a class="num" onclick="changePage('+index+')">&nbsp;'+index+'&nbsp;</a>';	


				}
				pageList += '<a class="gonext" onclick="'+(cateData.page+1)+'"><i class="iconfont" data-code="xe611"></i></a>';
				$('#pageList').html(pageList)
				return false;
			}


		})
		
	}









	var year = $('#year').val();
	var month = $('#month').val();
	var channelId = $('#channelId').val();

	//搜索效果
	$('.subnav_con .subnav_menu_ico').click(function(){
		$('.news_nav').toggleClass('active')
	})

	require('select');
	$('#selMonth').select();
	$('#selYear').select({
			callback:function(val,txt){
			 
				console.log("选中了",txt);
				$('#s_year').val(val);
				$.ajax({
					url: '/ext/ajax_getMonth.jsp',
					type: 'post',
					async:false,
					dataType: 'html',
					data: {
						channelId:channelId,
						year:val
				},
				success: function(data) {
					$('#selMonth').html(data);
					$('#selMonth').select();
				}
		 })
					
			}
	});
													
                    
			
  
});